using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Serialization.Formatters;

namespace Ruction
{
    internal class Server
    {
        private static TcpChannel mChannel = null;

        internal static Client StartUp()
        {
            BinaryServerFormatterSinkProvider serverProv = new BinaryServerFormatterSinkProvider();
            serverProv.TypeFilterLevel = TypeFilterLevel.Full;

            BinaryClientFormatterSinkProvider clientProv = new BinaryClientFormatterSinkProvider();

            Dictionary<string, object> props = new Dictionary<string, object>();
            props["port"] = Constants.DEFAULT_GAME_PORT;
            props["name"] = Constants.DEFAULT_GAME_HOST;


            mChannel = new TcpChannel(props, clientProv, serverProv);
            ChannelServices.RegisterChannel(mChannel);

            RemotingConfiguration.RegisterWellKnownServiceType(typeof(PlayHandler), "StratPlayHandler", WellKnownObjectMode.Singleton);
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(ChatHandler), "StratChatHandler", WellKnownObjectMode.Singleton);
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(MessageHandler), "StratMessageHandler", WellKnownObjectMode.Singleton);

            Client client = new Client();
            client.ConnectTo(Environment.MachineName, Constants.DEFAULT_GAME_PORT);

            return client;
        }

        internal static void ShutDown()
        {
            ChannelServices.UnregisterChannel(mChannel);
            mChannel = null;
        }
    }
}
